package com.mls.fivedistrict.controller;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.User;
import com.mls.fivedistrict.util.JsonUtils;
import com.mls.fivedistrict.util.jedis.RedisPoolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Base64Utils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by chenxiuxiang on 2018/1/1.
 */
public class BaseController {
    @Autowired
    protected HttpServletRequest request;
//
//    @ModelAttribute
//    public void init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
//        new Date();
//        System.out.println(new Date());
//        String filter = httpServletRequest.getHeader("pageFilter");
//        token = httpServletRequest.getHeader("token");
//        if (!StringUtils.isEmpty(filter)) {
//            ObjectMapper objectMapper = new ObjectMapper();
//            try {
//                pageFilter = JsonUtils.jsonByteToPojo(Base64Utils.decode(filter.getBytes()), PageFilter.class);
//                List<PageFilter.FilterBean> filters = pageFilter.getFilters();
//                if (filters != null) {
//                    filterMap = filters.stream().collect(Collectors.toMap(PageFilter.FilterBean::getKey, PageFilter.FilterBean::getValue));
//                } else {
//                    filterMap = new HashMap<>();
//                }
//                pageFilter.setFilterMap(filterMap);
//            } catch (Exception e) {
//                throw new CustomerException("pageFilter解析出错");
//
//            }
//        }
//}


    public PageFilter getPageFilter() {
        String filter = request.getHeader("pageFilter");
        PageFilter pageFilter = null;
        Map<String, Object> filterMap;
        if (!StringUtils.isEmpty(filter)) {
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                byte[] bytes = Base64Utils.decodeFromString(filter);
                String str = new String(bytes);
                pageFilter = JsonUtils.jsonToPojo(str, PageFilter.class);
                List<PageFilter.FilterBean> filters = pageFilter.getFilters();
                if (filters != null) {
                    filterMap = filters.stream().collect(Collectors.toMap(PageFilter.FilterBean::getKey, PageFilter.FilterBean::getValue));
                } else {
                    filterMap = new HashMap<>();
                }
                Map<String, String> stringFilterMap = new HashMap<>();
                filterMap.forEach((s, o) -> stringFilterMap.put(s, o.toString()));
                pageFilter.setFilterMap(stringFilterMap);
            } catch (Exception e) {
                throw new CustomerException("pageFilter解析出错");

            }
        }
        return Optional.ofNullable(pageFilter).orElse(new PageFilter());
    }


    public String getToken() {
        String token = request.getHeader("token");
        return token;
    }

    public User getUser() {
        String token = request.getHeader("token");
        if (!StringUtils.isEmpty(token)) {
            User user = JsonUtils.jsonToPojo(RedisPoolUtil.get(token), User.class);
            return user;
        }
        if (!StringUtils.isEmpty(token)) {
            User user = JsonUtils.jsonToPojo(RedisPoolUtil.get(token), User.class);
            return user;
        }
        return null;
    }


}
